package com.cdu.controller;

import com.cdu.annotation.OperationLogAnnotation;
import com.cdu.enums.OperationLogEnum;
import com.cdu.model.common.BaseResponse;
import com.cdu.model.query.NewsQuery;
import com.cdu.model.query.TaskQuery;
import com.cdu.service.NewsService;
import com.cdu.util.ParamUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;

/**
 * projectName : easy-help-v2
 *
 * @author :lql
 * date: 2022 -05 -23
 */
@RestController
@RequestMapping("/n")
@Api(value = "news", tags = "news - controller")
@Slf4j
@CrossOrigin
public class NewsController {
    @Autowired
    private NewsService newsService;

    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "Authentication", required = true),
    })
    @PostMapping("/create")
    @ApiOperation("创建新闻接口")
    @OperationLogAnnotation(getOperationAction = OperationLogEnum.CREATE)
    public BaseResponse create(HttpServletRequest request) throws ParseException {
        JSONObject ido = JSONObject.fromObject(ParamUtil.getAllParamMap(request));
        return newsService.createNews(ido);
    }

    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "Authentication", required = true),
    })
    @GetMapping("/queryAllNews")
    @ApiOperation("查询新闻接口")
    @OperationLogAnnotation(getOperationAction = OperationLogEnum.QUERY)
    public BaseResponse queryAllNews(HttpServletRequest request, NewsQuery newsQuery){
        return newsService.queryAllNews(newsQuery);
    }

    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "Authentication", required = true),
    })
    @GetMapping("/queryNewsById/{id}")
    @ApiOperation("通过id查询新闻接口")
    @OperationLogAnnotation(getOperationAction = OperationLogEnum.QUERY)
    public BaseResponse queryNewsById(HttpServletRequest request,@PathVariable(required = false) String id){
        NewsQuery newsQuery = new NewsQuery();
        newsQuery.setId(id);
        return newsService.queryNewById(newsQuery);
    }


    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "Authentication", required = true),
    })
    @PostMapping("/update")
    @ApiOperation("修改新闻接口")
    @OperationLogAnnotation(getOperationAction = OperationLogEnum.UPDATE)
    public BaseResponse update(HttpServletRequest request) throws ParseException {
        JSONObject ido = JSONObject.fromObject(ParamUtil.getAllParamMap(request));
        return newsService.updateNews(ido);
    }

    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "Authentication", required = true),
    })
    @GetMapping("/delNews/{id}")
    @ApiOperation("删除新闻接口")
    @OperationLogAnnotation(getOperationAction = OperationLogEnum.DELETE)
    public BaseResponse delNews(HttpServletRequest request,@PathVariable(required = false) String id){
        NewsQuery newsQuery = new NewsQuery();
        newsQuery.setId(id);
        return newsService.delNewsById(newsQuery);
    }


    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "Authentication", required = true),
    })
    @GetMapping("/detailNews/{id}")
    @ApiOperation("新闻详情接口(view+1)")
    @OperationLogAnnotation(getOperationAction = OperationLogEnum.QUERY)
    public BaseResponse detailNews(HttpServletRequest request,@PathVariable(required = false) String id){
        NewsQuery newsQuery = new NewsQuery();
        newsQuery.setId(id);
        return newsService.detailNews(newsQuery);
    }
}
